package tr.edu.yildiz.ce.dap.appagent.process;

import java.util.List;

import tr.edu.yildiz.ce.dap.appagent.exception.BasicAgentException;
import tr.edu.yildiz.ce.dap.appagent.manager.AgentProcessManager;
import tr.edu.yildiz.ce.dap.appagent.manager.ApplicationManager;
import tr.edu.yildiz.ce.dap.appagent.model.AppDefinition;
import tr.edu.yildiz.ce.dap.appagent.util.AgentProperties;
import tr.edu.yildiz.ce.dap.appagent.util.DeploymentUtils;


public class ProxyConnectionController extends MulticastReceiverProcess implements Runnable{

	public ProxyConnectionController(AgentProcessManager processManager,
			String multicastAddress, Integer port) {
		super(processManager, multicastAddress, port);
	}

	@Override
	protected void onMulticastPacketReceived(String message)
			throws BasicAgentException {
		
	}

	@Override
	protected void onMulticastConnectionFailed() throws BasicAgentException {
		if (AgentProperties.getInstance().isAgentUninstallWhenOffline()){
			List<AppDefinition> appDefList;
			try {
				appDefList = ApplicationManager.getAllAppDefinition();
			} catch (Exception e) {
				throw new BasicAgentException(e);
			}
			for (AppDefinition appDefinition : appDefList) {
				DeploymentUtils.uninstallApplication(appDefinition, getProcessManager().getContext());
			}
		}
	}

	
	
}
